package com.fjl.filewriter;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.zip.ZipFile;

import com.fjl.filewriter.PackerNg.Helper;

public class WriteMsgToApk
{
	public static void main(String[] args)
	{
		// MapGISExplorer1.1.8.apk
//		File file = new File("F:/MapGISExplorer.apk");// apk文件路径
//		File file = new File("F:/myapplication-release.apk");// ask文件路径
		File file = new File("F:/第三次土地利用外业调查系统20180816未上传.apk");// apk文件路径
		
		
//		writeApk(file, "fff");
//		writeApk(file, readFileToBytes("F:/author.xml"));

		
		try
		{
			Helper.writeZipComment(file, "2fjltest20180817");
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 将信息写入Android安装包.apk文件末尾
	 * 
	 * @param file 要写入信息的.apk文件
	 * @param comment 写入的信息
	 */
	public static void writeApk(File file, byte[] byteComment)
	{
		ZipFile zipFile = null;
		ByteArrayOutputStream outputStream = null;
		RandomAccessFile accessFile = null;
		try
		{
			zipFile = new ZipFile(file);
			String zipComment = zipFile.getComment();
			if (zipComment != null)
			{
				int s = zipComment.getBytes().length;
				write2file(zipComment, new File("F:/fjl.txt"));
			}
			
			int c = byteComment.length;

			outputStream = new ByteArrayOutputStream();

			outputStream.write(byteComment);
			outputStream.write(short2Stream((short) byteComment.length));

			byte[] data = outputStream.toByteArray();

			accessFile = new RandomAccessFile(file, "rw");
			accessFile.seek(file.length() - 2);
			accessFile.write(short2Stream((short) data.length));
			accessFile.write(data);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if (zipFile != null)
				{
					zipFile.close();
				}
				if (outputStream != null)
				{
					outputStream.close();
				}
				if (accessFile != null)
				{
					accessFile.close();
				}
			}
			catch (Exception e)
			{

			}

		}
	}

	/**
	 * 将信息写入Android安装包.apk文件末尾
	 * 
	 * @param file 要写入信息的.apk文件
	 * @param comment 写入的信息
	 */
	public static void writeApk(File file, String comment)
	{
		ZipFile zipFile = null;
		ByteArrayOutputStream outputStream = null;
		RandomAccessFile accessFile = null;
		try
		{
			zipFile = new ZipFile(file);
			String zipComment = zipFile.getComment();
//			if (zipComment != null)
//			{
//				return;
//			}
			int c = comment.getBytes().length;
			System.out.println(comment);
//			write2file(zipComment, new File("F:/fjl.txt"));
//			int s = zipComment.getBytes().length;
			

			byte[] byteComment = comment.getBytes();
			outputStream = new ByteArrayOutputStream();

			outputStream.write(byteComment);
			outputStream.write(short2Stream((short) byteComment.length));

			byte[] data = outputStream.toByteArray();

			accessFile = new RandomAccessFile(file, "rw");
			accessFile.seek(file.length() - 2);
			accessFile.write(short2Stream((short) data.length));
			accessFile.write(data);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if (zipFile != null)
				{
					zipFile.close();
				}
				if (outputStream != null)
				{
					outputStream.close();
				}
				if (accessFile != null)
				{
					accessFile.close();
				}
			}
			catch (Exception e)
			{

			}

		}
	}

	/**
	 * short转换成字节数组（小端序）
	 * 
	 * @param data
	 * @return
	 */
	private static short stream2Short(byte[] stream, int offset)
	{
		ByteBuffer buffer = ByteBuffer.allocate(2);
		buffer.order(ByteOrder.LITTLE_ENDIAN);
		buffer.put(stream[offset]);
		buffer.put(stream[offset + 1]);
		return buffer.getShort(0);
	}

	/**
	 * 字节数组转换成short（小端序）
	 * 
	 * @param stream
	 * @param offset
	 * @return
	 */
	private static byte[] short2Stream(short data)
	{
		ByteBuffer buffer = ByteBuffer.allocate(2);
		buffer.order(ByteOrder.LITTLE_ENDIAN);
		buffer.putShort(data);
		buffer.flip();
		return buffer.array();
	}
	
	/**
	 * 读文件的得到文件字节流
	 * @param filePath
	 * @return
	 */
	public static byte[] readFileToBytes(String filePath){
		byte[] bytes = null;
		try {
			BufferedInputStream bi = new BufferedInputStream(new FileInputStream(filePath));
			//输入流转字节流
			bytes = new byte[bi.available()];
			bi.read(bytes);
			bi.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bytes;
		
	}
	
	// 追加到文件尾
	public static void write2file(String string, File file) {
		String context = string + "\r\n";
		try {
			FileOutputStream fOutputStream = new FileOutputStream(file, false);// 设置为true，可以追加
			fOutputStream.write(context.getBytes());
			fOutputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
